/*
 * algoritmoBuscaLocal.cpp
 *
 *  Created on: Nov 11, 2012
 *      Author: ra083681
 */

#include "algoritmoBuscaLocal.h"

algoritmoBuscaLocal::algoritmoBuscaLocal() {

}

algoritmoBuscaLocal::~algoritmoBuscaLocal() {

}

double algoritmoBuscaLocal::buscaLocal(std::vector<ListGraph::Node> &current_solution, ListGraph &graph, double value_current_sol) {
	std::vector<ListGraph::Node> new_solution;

	Node nodeb;

	int k = 1;
	int melhorou = 0;

	while (k == 1) {
		k = 0; 
		if (current_solution.size() > 3) {
			for(unsigned i = 0; i < current_solution.size()-3; ++i) {
				int a = i;
				int b = i+1;
				int c = i+2;
				int d = i+3;

				//Se existirem arestas AB, BC, CD, AC, BD
				ListGraph::Edge ab = findEdge(*(&graph),current_solution[a],current_solution[b]);
				ListGraph::Edge cd = findEdge(*(&graph),current_solution[c],current_solution[d]);
				ListGraph::Edge ac = findEdge(*(&graph),current_solution[a],current_solution[c]);
				ListGraph::Edge bd = findEdge(*(&graph),current_solution[b],current_solution[d]);
				ListGraph::Edge bc = findEdge(*(&graph),current_solution[b],current_solution[c]);

				if (ab != lemon::INVALID && cd != lemon::INVALID && ac != lemon::INVALID && bd != lemon::INVALID && bc!= lemon::INVALID) {
					if ((SteinerConstrainedPathSolver::length[ab]+SteinerConstrainedPathSolver::length[cd]) > (SteinerConstrainedPathSolver::length[ac]+SteinerConstrainedPathSolver::length[bd])) {
						//Achou uma solução melhor

						printf("aqui\n");
						return 0;

						k = 1;
						melhorou = 1;

						nodeb = current_solution[i+1];
						current_solution[i+1] = current_solution[i+2];
						current_solution[i+2] = nodeb;

						//DEBUG MODE
						ListGraph::Edge e = findEdge(graph,current_solution[i],current_solution[i+1]); //ac
						ListGraph::Edge e1 = findEdge(graph,current_solution[i+1],current_solution[i+2]); //cb
						ListGraph::Edge e2 = findEdge(graph,current_solution[i+2],current_solution[i+3]); //bd

						double value_current_sol3 = 0;
						for(unsigned j = 0; j < current_solution.size()-1; ++j) {
							ListGraph::Edge e = findEdge(graph,current_solution[j],current_solution[j+1]);
							if(e != lemon::INVALID){
								value_current_sol3 += SteinerConstrainedPathSolver::length[e];
							}
							else{
								printf("ERRO");
							}
						}

						//printf("Solution value: %f\n",value_current_sol2);
						//printf("Solution value improved: %f\n",value_current_sol3);
						//END DEBUG
					}
				}

			}
		}
	}

	return melhorou;

}

